package com.hema.auction.activity;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.hema.auction.R;
import com.hema.auction.base.BaseActivity;
import com.hema.auction.fragment.MessageFragment;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 消息页面
 * author by john
 */

public class MessageActivity extends BaseActivity {

    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_message)
    TextView tvMessage;
    @BindView(R.id.tv_notice)
    TextView tvNotice;
    @BindView(R.id.iv_message)
    ImageView ivMessage;
    @BindView(R.id.tv_new_message)
    TextView tvNewMessage;
    @BindView(R.id.rl_message)
    RelativeLayout rlMessage;
    private MessageFragment messageFragment;
    private MessageFragment noticeFragment;
    private FragmentManager fragmentManager;
    private FragmentTransaction transaction;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_message);
        ButterKnife.bind(this);
        fragmentManager = getSupportFragmentManager();
        tvMessage.performClick();
    }


    private void changeFragment(View v) {
        transaction = fragmentManager.beginTransaction();
        switch (v.getId()) {
            case R.id.tv_message:
                if (messageFragment == null) {
                    messageFragment = MessageFragment.newInstance(0);
                    transaction.add(R.id.fragment, messageFragment);
                } else {
                    transaction.show(messageFragment);
                }
                if (noticeFragment != null) transaction.hide(noticeFragment);
                break;
            case R.id.tv_notice:
                if (noticeFragment == null) {
                    noticeFragment = MessageFragment.newInstance(1);
                    transaction.add(R.id.fragment, noticeFragment);
                } else {
                    transaction.show(noticeFragment);
                }
                if (messageFragment != null) transaction.hide(messageFragment);
                break;
        }
        transaction.commitAllowingStateLoss();

    }

    @OnClick({R.id.iv_back,R.id.tv_message, R.id.tv_notice})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_message:
                tvMessage.setEnabled(false);
                tvNotice.setEnabled(true);
                changeFragment(v);
                break;
            case R.id.tv_notice:
                tvMessage.setEnabled(true);
                tvNotice.setEnabled(false);
                changeFragment(v);
                break;
        }

    }

}
